<?php

class ctrl_wish_purchaseUrl extends L {

    /**
     * 描述 ：查询采购链接页面展示
     * 作者 ：Zoro.ZHu
    **/
    public function index(){
        empty($_GET['sku']) && $_GET['sku'] = '';
        $name = core_base::getUser('name');
        of_base_com_kv::set('purchaseUrlSpu' . '::' . $name, $_GET['sku']);
        $this->view->_pagingHtml = $this->paging();
        $this->view->headTitle = '查询采购链接';
        $this->display('/html/wish/purchaseUrl.html');
    }

    /**
     * 描述 : 获取分页
     * 作者 : Zoro.ZHu
     */
    public function &paging($params = array()){
        $sql = model_purchaseUrl::searchSql($params);
        $config = array(
            'itemId' => '{`itemId`}',
            '采购链接' => '{`_purchaseUrl`}',
            '中文申报名称<br>英文申报名称' => '{`nameCn`}<br>{`nameEn`}',
            '质检重量<br>平均重量<br>体积重' => '{`scalageWeight`}<br>{`avgWeight`}<br>{`volumeWeight`}',
            '供应商信息' => '{`_seller`}',
            '产品信息' => '{`_sku`}',
            '_attr' => array(
                "attr" => array (
                    "table" => 'id=purchaseUrlList class="table table-hover table-condensed table-striped table-scroll jsHoverDetails textC"',
                    "btr" => "align='center' class='of-paging_item_odd tr_{`itemId`}'",
                ),
                'data' => &$sql,
                'params' => &$params,
                'call' => array($this, 'asCall'),
                'method' => __METHOD__,
//                'save' => 'auto',
                'empty' => '查无此数据'
            )
        );
        return $this->_com->paging($config);
    }

    /**
     * 描述 : 整理数据
     * 作者 : Zoro.ZHu
     */
    public function asCall(&$data){
        foreach($data['data'] as &$val){
            $purchaseUrl = json_decode($val['purchaseUrl'], true);
            $skuInfo = json_decode($val['data'], true);
            $val['nameCn'] = !empty($skuInfo['nameCn']) ? $skuInfo['nameCn'] : '';//中文申报名称
            $val['nameEn'] = !empty($skuInfo['nameEn']) ? $skuInfo['nameEn'] : '';//英文申报名称
            $val['avgWeight'] = !empty($skuInfo['avgWeight']) ? $skuInfo['avgWeight'] : ''; //平均重量
            $val['volumeWeight'] = !empty($skuInfo['volumeWeight']) ? $skuInfo['volumeWeight'] : ''; //体积重
            $val['scalageWeight'] = !empty($skuInfo['scalageWeight']) ? $skuInfo['scalageWeight'] : ''; //质检重量
            $a = '';
            foreach ($purchaseUrl as $k => $u) {
                $a .= "<a href='{$u}' target=\"_blank\">采购链接" . ($k + 1) . "</a> ";
            }
            !empty($val['clearance']) && $a .= "<a href='https://www.wholesale24hours.com/s/{$val['sku']}' target=\"_blank\">清仓网站</a>";
            $val['_purchaseUrl'] = $a;
            if(!empty($skuInfo['seller'])){
                $val['_seller'] = "<table width='100%'><tr style='text-align: center'><td>供应商名称</td><td>报价</td><td>最低采购量</td></tr>";
                foreach($skuInfo['seller'] as $s){
                    $val['_seller'] .= "<tr style='text-align: center'><td><a href='{$s['sellerUrl']}' target=\"_blank\">{$s['sellerName']}</a></td><td>{$s['sellerPrice']}</td><td>{$s['minQty']}</td></tr>";
                }
                $val['_seller'] .= "</table>";
            }
            if(!empty($skuInfo['skuAttr'])){
                $val['_sku'] = "<table width='100%'><tr style='text-align: center'><td>商品名称</td><td>包裹清单</td><td>采购价(￥)</td><td>建议售价($)</td><td>重量(g)</td><td>长宽高(cm)</td></tr>";
                foreach($skuInfo['skuAttr'] as $s){
                    $val['_sku'] .= "<tr style='text-align: center'><td>{$s['oldNumber']}</td><td>{$s['packing']}</td><td>{$s['price']}</td><td>{$s['shopPrice']}</td><td>{$s['weight']}</td><td>{$s['lwh']}</td></tr>";
                }
                $val['_sku'] .= "</table>";
            }
        }
    }

    /**
     * 描述 : 新增和修改的页面展示
     * 作者 : Zoro.ZHu
     */
    public function purchaseUrlUpInfo(){
        if(!empty($_REQUEST['itemId'])) {
            $purchaseUrl = model_purchaseUrl::getKeyInfo($_REQUEST['itemId']);
            if(!empty($purchaseUrl)){ 
                $this->view->itemId = $purchaseUrl[0]['itemId'];
                $this->view->url = $purchaseUrl[0]['url'];
                $this->view->sku = $purchaseUrl[0]['sku'];
                $this->view->purchaseUrl = $purchaseUrl[0]['purchaseUrl'];
                $this->view->addTime = $purchaseUrl[0]['addTime'];
                $this->view->data = $purchaseUrl[0]['data'];
            }
        }
        $this->display('/html/purchaseUrlUpInfo.html');
    }

    /**
     * 描述 ：新增或修改数据
     * 作者 ：Zoro.ZHu
    **/
    public function purchaseUrlUpd(){
        //主键为空是新增，否则就是修改
        if(empty($_POST['itemId'])){
            $result = model_purchaseUrl::insertSql($_POST);
        }else{
            $result = model_purchaseUrl::updateSql($_POST);
        }
        if($result > 0){
            return array('state' => 200, 'info' => '操作成功');
        }else{
            return array('state' => 301, 'info' => '操作无效');
        }
    }

    /**
     * 描述 ：按条件导出数据
     * 作者 ：Zoro.ZHu
    **/
    public function exportCsv(){
        $sql = model_purchaseUrl::searchSql(array('search' => $_GET));
        $data = L::sql($sql);
        of_base_com_csv::download('导出数据' . time());                     //导出数据的文件名
        of_base_com_csv::download(array('itemId','采购链接','其它信息',));
        foreach ($data as $row){
            of_base_com_csv::download(array($row['itemId'],$row['purchaseUrl'],$row['data'],));
        }
    }

    /**
     * 描述 : 匹配采购链接
     * 作者 : Zoro.Zhu
     */
    public function getPurchaseUrl(){
        if(empty($_POST['url'])){
            return ['state' => 300, 'info' => 'url不能为空'];
        }
        $purchaseUrl = ctrl_wish_product::getPurchaseUrl(trim($_POST['url']));
        if(!empty($purchaseUrl)){
            return ['state' => 200, 'info' => '匹配成功'];
        } else {
            return ['state' => 301, 'info' => '匹配失败'];
        }
    }

}
return true;